<?php
session_start();
$page_name=basename($_SERVER['SCRIPT_FILENAME']);
$IsPreserved	= 'Y';
$IsProcess		= $_REQUEST['IsProcess'];
$action  =  $_REQUEST['action'];
$action_arr = array("getSrchResult");

$game_id = $_REQUEST['game_id']; // Added By Chandan


if(in_array($action,$action_arr))
{
	$IsProcess = 'Y';
}
include("general_include.php");

$sort_by = $_REQUEST['sorting_by'];
$list_for = $_REQUEST['list_for'];
$username = $_REQUEST['unm'];

// EDIT BY TUSHAR ------------ //
//$availability_country = $_REQUEST['availability_country'];
$availability_city = $_REQUEST['availability_city'];

$ladder_id  = $_REQUEST['ladder_id'];
$server_id  =$_REQUEST['server_id'];
$race_id  = $_REQUEST['race_id'];
$region_id  = $_REQUEST['region_id'];
$rating_id  = $_REQUEST['rating_id'];
$availability  = $_REQUEST['availability'];
$language_id  = $_REQUEST['language_id'];
	
$price  = $_REQUEST['price'];

// EDIT BY TUSHAR ------[END]------ //


/*if($ladder_id == '')
   $ladder_id =array();
if(empty($race_id))
   $race_id = array();
if($server_id == '')
   $server_id =array();
if($region_id == '')
   $region_id =array();
if($rating_id == '')
   $rating_id =array();
if($availability == '')
   $availability = array();   
if($language_id == '')
  $language_id = array();*/
if($price == '')
  $price = '0;100';
  
if(count($ladder_id)>0&& is_array($ladder_id)&& !empty($ladder_id))
{
	$ladder_ids = implode(',',$ladder_id);
	$smarty->assign('ladders',$ladder_ids);
	$sqlPart .= " AND ug.ladder_id IN($ladder_ids)";
}
if(count($race_id)>0&& is_array($race_id)&& !empty($race_id))
{
	$race_ids = implode(',',$race_id);
	$smarty->assign('races',$race_ids);
	foreach($race_id as $val)
	$sqlPart .= " AND FIND_IN_SET($val,ug.race_id)";
}
if(count($server_id)>0&& is_array($server_id)&& !empty($server_id))
{
	$server_ids = implode(',',$server_id);
	$smarty->assign('servers',$server_ids);
	foreach($server_id as $val)
	$sqlPart .= " AND FIND_IN_SET($val,ug.server_id)";
}
if(count($region_id)>0&& is_array($region_id)&& !empty($region_id))
{
	$region_ids = implode(',',$region_id);
	$smarty->assign('regions',$region_ids);
	foreach($region_id as $val)
	$sqlPart .= " AND FIND_IN_SET($val,ug.region_id)";
}
if(count($rating_id)>0&& is_array($rating_id)&& !empty($rating_id))
{
	$rating_ids = implode(',',$rating_id);
	$smarty->assign('ratings',$rating_ids);
	foreach($rating_id as $val)
	$sqlPart .= " AND FIND_IN_SET($val,ug.rating_id)";
}
if(!empty($price))
{
   $price_range = explode(";",$price);
   $min_price = $price_range[0];
   $max_price = $price_range[1];
   if($min_price == '0' && $max_price == '100')
    $sqlPart .='';
   else if($min_price == '0' && $max_price <100)
    $sqlPart .=" AND ug.rate <='$max_price'";
   else if($min_price >'0' && $max_price == '100')
    $sqlPart .=" AND ug.rate >='$min_price'";
   else 
     $sqlPart .=" AND ug.rate BETWEEN '$min_price' AND '$max_price'";		  
}   
if(count($language_id)>0&& is_array($language_id)&& !empty($language_id))
{
	$language_ids = implode(',',$language_id);
	$smarty->assign('languages',$language_ids);
	foreach($language_id as $val)
	$sqlPart .= " AND FIND_IN_SET($val,u.language_ids)";
}
if(count($availability)>0&& is_array($availability)&& !empty($availability))
{
	$availabilitys = implode(',',$availability);
	$smarty->assign('availabilitys',$availabilitys);
	foreach($availability as $val)
	$sqlPart .= " AND FIND_IN_SET('$val',u.availability_type)";
}
if(!empty($availability_country))
{
   $sqlPart .= " AND u.availability_country ='$availability_country'";
}
if(!empty($availability_city))
{
   $sqlPart .= " AND u.availability_city ='$availability_city'";
}

if(!empty($username))
{
  $sqlPart .= " AND (u.name like '$username%' OR u.description like '%$username%' OR ug.lesson_plan like '%$username%' OR ug.experience like '$username%')";
}
if($game_id=='')
{
   $Sql = "SELECT * FROM ".TABLEPREFIX."_game WHERE is_default='Y'";
   $idRow = $UserManagerObjAjax->GetRecords("Row",$Sql);
   $game_id = $idRow['game_id'];
 }  

   $GameSql = "SELECT * FROM ".TABLEPREFIX."_game WHERE game_id='$game_id'";  
   $gamerow = $UserManagerObjAjax->GetRecords("Row",$GameSql);
   $game_id = $gamerow['game_id'];
   $game_name = $gamerow['game_name'];
   $is_ladder = $gamerow['is_ladder'];
   $is_race = $gamerow['is_race'];
   $is_server = $gamerow['is_server'];
   $is_region = $gamerow['is_region'];
   $is_rating = $gamerow['is_rating'];


function findlanguage($language_id)
{
     if($language_id=='')
			return false;
			
		$SelectTypeSql = "SELECT language_name FROM ".TABLEPREFIX."_language WHERE language_id=$language_id";
		$SelectType = mysql_fetch_array(mysql_query($SelectTypeSql));
		return $SelectType[0];
}		
function findvalue($table,$fld_name,$fld_value,$find_value)
{
     if($fld_value=='')
			return false;
			
	    $SelectTypeSql = "SELECT ".$find_value." FROM ".$table." WHERE $fld_name=".$fld_value;
		$SelectType = mysql_fetch_array(mysql_query($SelectTypeSql));
		return $SelectType[0];
}	

$record_per_page = !empty($record_per_page)?$record_per_page:15;
$paginationObj = new PaginationClassFrontAjax($record_per_page,3,1,'Next','Previous');

//echo $list_for;
//echo $sort_by;

if($IsProcess != 'Y')
 {
    if($list_for == 'sorting')
	{
	   $sortBy = explode('-',$sort_by);
	  
	  if($sortBy[0] == 'rating')
	  {
		if($sortBy[1]=="DESC")
		 $sqlPart.=" ORDER BY ur.overall_rating DESC,u.name ASC";
		else 
		 $sqlPart.=" ORDER BY ur.overall_rating ASC,u.name ASC";
	  }	
	 
   }
   else 
        $sqlPart.=" ORDER BY u.date_registered DESC";
	 
		$smarty->assign('sort_by',$sort_by);
		$_SESSION['sql'] = $sqlPart;
		$_SESSION['sort'] = $sort_by;
		$SelectcoachSQL="SELECT DISTINCT u.*,ur.*,ug.* FROM ".TABLEPREFIX."_user u ,".TABLEPREFIX."_user_type_user_relation ur , ".TABLEPREFIX."_user_game ug  
		WHERE ur.user_type_id=3 and ur.user_id=u.user_id and u.is_active='Y' and ug.user_id=u.user_id and ug.game_id=$game_id and ug.is_active='Y' and ug.user_type_id=3".$sqlPart;
   }	
   else 
   {
	   $coachSQl = $_SESSION['sql'];
	   $smarty->assign('sort_by',$_SESSION['sort']);
	   $SelectcoachSQL="SELECT DISTINCT u.*,ur.*,ug.*,g.* FROM ".TABLEPREFIX."_user u ,".TABLEPREFIX."_user_type_user_relation ur , ".TABLEPREFIX."_user_game ug, ".TABLEPREFIX."_game g WHERE ur.user_type_id=3 and g.user_id=u.user_id and ur.user_id=u.user_id and u.is_active='Y' and ug.user_id=u.user_id and ug.game_id=$game_id and ug.is_active='Y' and ug.user_type_id=3".$coachSQl;
   }
 $pagination_arr = $paginationObj->PaginationFrontAjaxALL('PaginationSearch',$SelectcoachSQL,$page_name.'?'.$catLink);
 
 $CoachArr=$UserManagerObjAjax->GetRecords("All",$pagination_arr[0]);
	//echo $pagination_arr[0];

 $NumCoach=count($CoachArr);
	
for($i=0;$i<$NumCoach;$i++)
{
	$CoachArr[$i]['name']=show_to_control($CoachArr[$i]['name']);
	$CoachArr[$i]['about']=show_to_control($CoachArr[$i]['user_about']);
	$Clanguage = explode(",",$CoachArr[$i]['language_ids']);
	$language='';
	for($l=0;$l<count($Clanguage);$l++)
	{
	   if($Clanguage[$l]<>'')
	    $language .=findlanguage($Clanguage[$l]).' , ';
	}
	$CoachArr[$i]['language']=substr($language,0,-2);   
	$CoachArr[$i]['ladder']=findvalue(TABLEPREFIX."_game_ladder","ladder_id",$CoachArr[$i]['ladder_id'],"ladder_name");
	$Crace = explode(",",$CoachArr[$i]['race_id']);
	$race='';
	for($r=0;$r<count($Crace);$r++)
	{  
	  if($Crace[$r]<>'')
	   $race .= findvalue(TABLEPREFIX."_game_race","race_id",$Crace[$r],"race_title").' , ';
	}  
	$CoachArr[$i]['race']=substr($race,0,-2);  
	$Cserver = explode(",",$CoachArr[$i]['server_id']);
	$server='';
	for($s=0;$s<count($Cserver);$s++)
	{
	  if($Cserver[$s]<>'')
	   $server .=findvalue(TABLEPREFIX."_game_server","server_id",$Cserver[$s],"server_name");
	}  
	$CoachArr[$i]['server']=substr($server,0,-1);
	$Cregion = explode(",",$CoachArr[$i]['region_id']);
	$region='';
	for($x=0;$x<count($Cregion);$x++)
	{  
	  if($Cregion[$x]<>'')
	   $region .= findvalue(TABLEPREFIX."_game_region","region_id",$Cregion[$x],"region_title").' , ';
	}  
	$CoachArr[$i]['region']=substr($region,0,-2);
	$Crating = explode(",",$CoachArr[$i]['rating_id']);
	$rating='';
	for($x=0;$x<count($Crating);$x++)
	{  
	  if($Crating[$x]<>'')
	   $rating .= findvalue(TABLEPREFIX."_game_rating","rating_id",$Crating[$x],"rating_title").' , ';
	}  
	$CoachArr[$i]['rating']=substr($rating,0,-2);
	
	
	$avail_type = explode(',',$CoachArr[$i]['availability_type']);
	if($avail_type[1]=='')
	{
	  if($avail_type[0]=='O')
	   $CoachArr[$i]['availability_type'] = 'Online'; 
	  if($avail_type[0]=='L')
	  {
	   $CoachArr[$i]['availability_type'] = 'Local meet-up'; 
	   $CoachArr[$i]['avail_local'] = 'Y';
	  } 
	}
	else
	{
	   $CoachArr[$i]['availability_type'] = 'Online , Local meet-up';
	   $CoachArr[$i]['avail_local'] = 'Y';
	}   
	
	$CoachArr[$i]['availability_country'] = findvalue(TABLEPREFIX."_country","country_id",$CoachArr[$i]['availability_country'],"country_name");
	$CoachArr[$i]['availability_city'] = findvalue(TABLEPREFIX."_cities","city_id",$CoachArr[$i]['availability_city'],"city_key");

$Sql_overall_tooptip="select rating_overall_tooltip from ".TABLEPREFIX."_default_comision";
	$overall_tooptip=$UserManagerObjAjax->GetRecords("Row",$Sql_overall_tooptip);
	$CoachArr[$i]['rating_overall_tooltip']=html_entity_decode($overall_tooptip['rating_overall_tooltip']);
	
	$totalReviewSql = "SELECT count(*) FROM ".TABLEPREFIX."_user_review WHERE user_id=".$CoachArr[$i]['user_id']." AND user_type_id=3";
	$totalR = $UserManagerObjAjax->GetRecords("Row",$totalReviewSql);
	$CoachArr[$i]['totalReview'] = $totalR[0];
	
	$Sql_Individual="select avg(rat.rating) as tot,rat.rcat_id,cat.rating_category,cat.rating_category_tooltip from ".TABLEPREFIX."_user_rating as rat,".TABLEPREFIX."_user_review as rev,".TABLEPREFIX."_rating_category as cat where rat.rcat_id=cat.rcat_id and rat.user_review_id=rev.user_review_id and rev.user_id=".$CoachArr[$i]['user_id']." and rev.user_type_id=3 group by rat.rcat_id";
	$Individual=$UserManagerObjAjax->GetRecords("All",$Sql_Individual);

for($j=0;$j<count($Individual);$j++){

	$CatIndividualRec[$i][$j]['rating']=number_format($Individual[$j]['tot'], 1, '.', '');
	$CatIndividualRec[$i][$j]['rcat_id']=$Individual[$j]['rcat_id'];
	$CatIndividualRec[$i][$j]['rating_category']=$Individual[$j]['rating_category'];
	$CatIndividualRec[$i][$j]['rating_category_tooltip']=html_entity_decode($Individual[$j]['rating_category_tooltip']);
}
	
/*echo '<pre>';
print_r($CatIndividualRec);*/
}
$city = findvalue(TABLEPREFIX."_cities","city_id",$availability_city,"city_key");

$gameSql = "SELECT game_id,game_name FROM ".TABLEPREFIX."_game where is_active='Y' ORDER BY game_name";
$GameArr = $UserManagerObjAjax->HtmlOptionArrayCreate($gameSql);
//print_r($ladder_id);
$smarty->assign('username',$username);
$smarty->assign('ladder_id',$ladder_id);
$smarty->assign('race_id',$race_id);
$smarty->assign('server_id',$server_id);
$smarty->assign('region_id',$region_id);
$smarty->assign('rating_id',$rating_id);
$smarty->assign('language_id',$language_id);
$smarty->assign('availability',$availability);
$smarty->assign('price',$price);
$smarty->assign("SearchTxt",$SearchTxt);
$smarty->assign("GameArr",$GameArr);
$smarty->assign('page_name',$page_name);
//$smarty->assign('sort_by',$sort_by);
$smarty->assign('game_id',$game_id);
$smarty->assign('game_name',$game_name);
$smarty->assign('Numladder',$Numladder);
$smarty->assign('LadderArr',$LadderArr);
$smarty->assign('Numrace',$Numrace);
$smarty->assign('RaceArr',$RaceArr);
$smarty->assign('city',$city);
$smarty->assign('cityid',$availability_city);
//$smarty->assign('availability_country',$availability_country);
$smarty->assign('availability_city',$availability_city);
$smarty->assign('Numserver',$Numserver);
$smarty->assign('ServerArr',$ServerArr);
$smarty->assign('Numlanguage',$Numlanguage);
$smarty->assign('LangArr',$LangArr);
$smarty->assign('StarArr',$StarArr);
$smarty->assign("CoachArr",$CoachArr);
$smarty->assign('pagination_arr',$pagination_arr);	
$smarty->assign("NumCoach",$NumCoach);
$smarty->assign("CatIndividualRec",$CatIndividualRec);
if($IsProcess <> 'Y') 
  include "training_partner_leftpanel.php";
$smarty->display('find_training_partner.tpl');
//if($IsProcess <> 'Y') include "footer.php";

?>